package com.timeriver.web;

import com.timeriver.api.PredictService;
import com.timeriver.domain.PredictRequest;
import com.timeriver.domain.ResponseObj;
import com.timeriver.utils.ResponseUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest")
public class PredictController {

    @Reference(version = "${user.service.version}")
    private PredictService predict;

    @PostMapping("/predict")
    public ResponseObj predict(@RequestBody PredictRequest request) {
        Double prediction;
        try {
            prediction = predict.getPrediction(request);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage());
        }

        if (prediction == null) {
            return ResponseUtil.error(500, "响应失败");
        }

        return ResponseUtil.success(prediction);
    }
}
